package com.tjtt.tdm.fileupload.service.impl;

import java.util.List;
import java.util.Map;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.fileupload.bean.TdmAttachment;
import com.tjtt.tdm.fileupload.dao.TdmAttachmentDao;
import com.tjtt.tdm.fileupload.service.TdmAttachmentService;

public class TdmAttachmentServiceImpl implements TdmAttachmentService {

	private TdmAttachmentDao tdmAttachmentDao;
	
	public TdmAttachmentDao getTdmAttachmentDao() {
		return tdmAttachmentDao;
	}

	public void setTdmAttachmentDao(TdmAttachmentDao tdmAttachmentDao) {
		this.tdmAttachmentDao = tdmAttachmentDao;
	}

	public List<TdmAttachment> getAttachment(String codeid,String column) throws DaoException {
		String hql = "from TdmAttachment t where 1=1 ";
		hql +=" and t.codeId=" + codeid;
//		if(!StringUtil.isEmpty(codeid)){
//			hql +=" and t.codeId=" + codeid;
//		}else{
//			hql +=" and t.codeId=0";
//		}
		if(!StringUtil.isEmpty(column)){
			hql +=" and t.columnName='" + column + "'";
		}
			hql += " order by t.createTime asc";
		return tdmAttachmentDao.findByHql(hql);
	}
	@Override
	public PageResult queryAttachment(Map filterMap) throws DaoException {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) "+
							 " FROM TDM_ATTACHMENT T where 1=1 ");
		queryString.append("SELECT T.ID, T.FILE_NAME, T.FILE_TYPE,"+
						      " T.FILE_VERSION,"+
						      " T.FILE_PATH,"+
						      " T.FILE_DESC,"+
						      " T.CREATE_TIME,"+
						      " T.CREATE_USERID,"+
						      " T.CREATE_USERNAME,"+
						      " T.CODE_ID,"+
						      " T.FILE_LENGTH"+
							  " FROM TDM_ATTACHMENT T"+
							  " where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			String filterProperty;
			String filterValue;
			//type 1：为申请人，2：试件负责人，3：试验室负责人，4：质量主管，5：结构主管，6：技术负责人，7：基层单位领导
			//type 8：承试单位领导，9：主管军代表
			filterProperty = "code_id";
			filterValue = (String) filterMap.get(filterProperty);
			if (!StringUtil.isMissing(filterValue)) {
				queryString.append(" and T.CODE_ID").append(" =").append(filterValue);
				queryCountString.append(" and T.CODE_ID").append(" =").append(filterValue);
			} 
			filterProperty = "column";
			filterValue = (String) filterMap.get(filterProperty);
			if (!StringUtil.isMissing(filterValue)) {
				queryString.append(" and T.COLUMN_NAME").append(" ='").append(filterValue).append("'");
				queryCountString.append(" and T.COLUMN_NAME").append(" ='").append(filterValue).append("'");
			} 
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		}
		return tdmAttachmentDao.searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	@Override
	public String saveAttachement(TdmAttachment attachment) throws DaoException {
		String id = tdmAttachmentDao.saveObject(attachment);
		return id;
	}

	@Override
	public TdmAttachment getOneAttachment(String id) throws DaoException {
		return tdmAttachmentDao.get(id);
	}

	@Override
	public void deleteAttachment(TdmAttachment attachment) throws DaoException {
		
		tdmAttachmentDao.remove(attachment);
		
	}
 
 
	
}
